package com.yyq.adms.yyb.patent.bpo.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yyq.adms.util.JxlsUtil;
import com.yyq.adms.util.JxlsVo;
import com.yyq.adms.yyb.patent.bpo.PatentApplyBpo;
import com.yyq.adms.yyb.patent.dao.PatentApplyDao;
import com.yyq.adms.yyb.patent.pojo.PatentApply;
import com.yyq.sos.service.BaseBpoImpl;


/**
 * 专利申请  业务逻辑类
 * @author 杨应滨
 * @2012-12-3
 *
 */
@Service("patentApplyBpo")
@Transactional
public class PatentApplyBpoImpl  extends BaseBpoImpl implements PatentApplyBpo{

	private PatentApplyDao dao;
	private static final String dateFormat = "yyyy-MM-dd";
	
	@Resource(name="patentApplyDao")
	public void setDao(PatentApplyDao dao) {
		this.dao = dao;
		super.setBaseDao(dao);
	}

	public String validateBatchAdd(List<Map<String, JxlsVo>> data) {
		List list = new ArrayList();
		//String msg = JxlsUtil.getInstance().validateXls(data, vMap, po.getClass(), list);
		List<String> validateUnique = new ArrayList<String>();
		StringBuffer errMsg = new StringBuffer();
		for (Map<String, JxlsVo> map : data) {
			PatentApply obj = new PatentApply();
//专利名称	申请号	专利类型	申请人(内部)	申请人(外部)	发明人	内容修改负责人	申请方式	申请时间	提交时间	受理时间	
			//补正	初审合格发放时间	实审时间	一通时间	二通时间	三通时间	当前状态	(未)取得时间	所属平台
			JxlsVo nameJVo = map.get("专利名称");
			JxlsVo applyCodeJVo = map.get("申请号");
			JxlsVo patentTypeJVo = map.get("专利类型");
			JxlsVo applicantInJVo = map.get("申请人(内部)");
			JxlsVo applicantOutJVo = map.get("申请人(外部)");
			JxlsVo inventorJVo = map.get("发明人");
			JxlsVo modifyPersonJVo = map.get("内容修改负责人");
			JxlsVo applyTypeJVo = map.get("申请方式");
			JxlsVo applyDateJVo = map.get("申请时间");
			JxlsVo submitDateJVo = map.get("提交时间");
			JxlsVo dealDateJVo = map.get("受理时间");
			JxlsVo modifyJVo = map.get("补正");
			JxlsVo makeSureDateJVo = map.get("初审合格发放时间");
			JxlsVo realDealDateJVo = map.get("实审时间");
			JxlsVo firstDateJVo = map.get("一通时间");
			JxlsVo secondDateJVo = map.get("二通时间");
			JxlsVo thirdDateJVo = map.get("三通时间");
			JxlsVo nowStateJVo = map.get("当前状态");
			JxlsVo resultDateJVo = map.get("(未)取得时间");
			JxlsVo belongPlateFormIDJVo = map.get("所属平台");
			
			validateBlank(errMsg, nameJVo,"专利名称");
			validateBlank(errMsg, patentTypeJVo,"专利类型");
			validateBlank(errMsg, applicantInJVo,"申请人(内部)");
			validateBlank(errMsg, applyTypeJVo,"申请方式");
			validateBlank(errMsg, applyDateJVo,"申请时间");
			validateBlank(errMsg, belongPlateFormIDJVo,"所属平台");
			
			validateDate(errMsg, applyDateJVo, "申请时间",dateFormat);
			validateDate(errMsg, submitDateJVo, "提交时间",dateFormat);
			validateDate(errMsg, dealDateJVo, "受理时间",dateFormat);
			validateDate(errMsg, makeSureDateJVo, "初审合格发放时间",dateFormat);
			validateDate(errMsg, realDealDateJVo, "实审时间",dateFormat);
			validateDate(errMsg, firstDateJVo, "一通时间",dateFormat);
			validateDate(errMsg, secondDateJVo, "二通时间",dateFormat);
			validateDate(errMsg, thirdDateJVo, "三通时间",dateFormat);
			validateDate(errMsg, resultDateJVo, "(未)取得时间",dateFormat);
			
			if(patentTypeJVo.getContent().equals("发明专利")){
				obj.setPatentType("1");
			}else if(patentTypeJVo.getContent().equals("实用新型专利")){
				obj.setPatentType("2");
			}else{
				JxlsUtil.preErrMsg(errMsg, patentTypeJVo, "专利类型");
				errMsg.append("只能填(发明专利、实用新型专利)").append("\n");
			}
			
			if(applyTypeJVo.getContent().equals("自主申请")){
				obj.setPatentType("1");
			}else if(applyTypeJVo.getContent().equals("通过代理")){
				obj.setPatentType("2");
			}else{
				JxlsUtil.preErrMsg(errMsg, applyTypeJVo, "申请方式");
				errMsg.append("只能填(自主申请、通过代理)").append("\n");
			}
			
			
			
			
			obj.setPatentName(map.get("专利名称").getContent());
			obj.setApplyCode(map.get("申请号").getContent());
			//JxlsVo 
		}
		return null;
	}

	private void validateBlank(StringBuffer errMsg, JxlsVo jVo,String key) {
		if(jVo.getContent().equals("")){
			JxlsUtil.preErrMsg(errMsg, jVo, key);
			errMsg.append("不能为空").append("\n");
		}
	}
	
	private void validateDate(StringBuffer errMsg, JxlsVo jVo,String key,String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		if(!jVo.getContent().equals("")){
			Date d = null;
			try {
				d = sdf.parse(jVo.getContent());
			} catch (ParseException e) {
				JxlsUtil.preErrMsg(errMsg, jVo, key);
				errMsg.append(jVo.getContent()).append(":").append("其日期格式不正确(").append(format)
				.append(")\n");
			}
			if(d != null){
				String temp = sdf.format(d);
				if(!temp.equals(jVo.getContent())){
					JxlsUtil.preErrMsg(errMsg, jVo, key);
					errMsg.append(jVo.getContent()).append(":").append("其日期格式不正确(").append(format)
					.append(")\n");
				}
			}
		}
	}

	public void validateUniquene(List<Map<String, JxlsVo>> data,StringBuffer sb) {
		Map<String,Integer> validate = new HashMap<String,Integer>();
		for (Map<String, JxlsVo> map : data) {
			String name = map.get("专利名称").getContent();
			String type = map.get("专利类型").getContent();
			if(validateUniqueneDB(name,type)){
				sb.append("第"+map.get("专利名称").getRow()+"行:(专利名称+专利类型)与数据库里数据重复了\n");
			}else{
				String key = name+type;
				if(validate.containsKey(key)){
					sb.append("第"+validate.get(key)+"行:(专利名称+专利类型)与第"+map.get("专利名称").getRow()+"行唯一性重复了\n");
				}else{
					validate.put(key, map.get("专利名称").getRow());
				}
			}
		}
	}
	
	private boolean validateUniqueneDB(String name,String type){
		
		
		String hql = "from PatentApply where patentName='"+name+"' and patentType='"+type+"'";
		long count = dao.count(hql);
		if(count > 0){
			return true;
		}
		return false;
	}
}
